#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
use File::Basename;
use Getopt::Long;

########  Declarations #######
my %options=(
			sid=>undef,
			lib=>"libnsrdb2.so",
			backup_command=>undef,
			sessions=>1,
			db2_update_log=>undef,
			db2_update_fail=>undef,
			lowercase_sid=>undef,
			nsr_log_file=>"/nsr/logs/update_db2_conf_and_backup.log",
			backup_result=>undef,
			update_log_result=>undef,
			update_fail_result=>undef,
			stopsap_result=>undef,
			db2start_result=>undef,
			startsap_result=>undef
			
);
`/usr/bin/touch $options{nsr_log_file}`;

GetOptions(
			"sid=s"=>\$options{sid},
			"sessions=s"=>\$options{sessions}		
);

if ($^O =~ /aix/){
	$options{lib}="libnsrdb2.o";
}
$options{lowercase_sid}=lc($options{sid});
$options{backup_command}="db2 backup db $options{sid} load \'/usr/lib/$options{lib}\' open $options{sessions} sessions options \'\@/nsr/res/nmdb2_$options{sid}.cfg\'";
$options{db2_update_log}="db2 update db cfg for $options{sid} using logarchmeth1 VENDOR:/usr/lib/$options{lib} logarchopt1 @/nsr/res/nmdb2_$options{sid}.cfg";
$options{db2_update_fail}="db2 update db cfg for $options{sid} using FAILARCHPATH /db2/$options{sid}/db2arch";

# sub for message logging - messages are print to command line and log file
sub logging {
 my $log_message = join(' ',@_); 
 open (LOG,">>$options{nsr_log_file}") or die "could not open log file $options{nsr_log_file}\n"; # open log-file
 print localtime()." ".$log_message."\n";	# print message with timestamp onto STDOUT (into commandline for manual started backups)
 print LOG localtime()." ".$log_message."\n"; # print message with timestamp into log-file
 close (LOG) or die "could not close log file $options{nsr_log_file}\n"; # close log-file
}

#### Main

&logging ("Starting DB2 in case it is not active");
$options{db2start_result}=`su - db2$options{lowercase_sid} -c db2start`; # start db2
chomp $options{db2start_result}; # deleting newline in return message
$options{db2start_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{db2start_result}"); # logfile entry

&logging ("Starting Backup configuration for system $options{sid}");
$options{update_log_result}=`su - db2$options{lowercase_sid} -c "$options{db2_update_log}"`; # update db2 config for logbackup
chomp $options{update_log_result}; # deleting newline in return message
$options{update_log_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{update_log_result}"); # logfile entry

$options{update_fail_result}=`su - db2$options{lowercase_sid} -c "$options{db2_update_fail}"`; # update db2 config for failarchpath
chomp $options{update_fail_result}; # deleting newline in return message
$options{update_fail_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{update_fail_result}"); # logfile entry

&logging ("Sap is shutting down");
$options{stopsap_result}=`su - $options{lowercase_sid}adm -c stopsap`; # stop sap system
chomp $options{stopsap_result}; # deleting newline in return message
$options{stopsap_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{stopsap_result}"); # logfile entry

&logging ("Starting DB2");
$options{db2start_result}=`su - db2$options{lowercase_sid} -c db2start`; # start db2
chomp $options{db2start_result}; # deleting newline in return message
$options{db2start_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{db2start_result}"); # logfile entry

&logging ("Starting offline backup");
$options{backup_result}=`su - db2$options{lowercase_sid} -c "$options{backup_command}"`; # do an offline backup of the db2
chomp $options{backup_result}; # deleting newline in return message
$options{backup_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{backup_result}"); # logfile entry

&logging ("Starting SAP again");
$options{startsap_result}=`su - $options{lowercase_sid}adm -c startsap`; # start sap system
chomp $options{startsap_result}; # deleting newline in return message
$options{startsap_result} =~ s/\s+/ /g; # replace mulitple spaces with one space
&logging ("$options{startsap_result}"); # logfile entry

&logging ("Backup Configuration finished ! ");